//
//  WXLogin.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/13.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class WXLogin: NSObject {
    static let shared = WXLogin()
    
    /**
     ## 实现微信第三方登陆
     */
    func wxObserver() -> Void {
        //通知名称常量
        let wxNotifyChatMsgRecv = NSNotification.Name(rawValue:"wxdelegate")
        
        //接受通知监听
        NotificationCenter.default.addObserver(self, selector:#selector(wxdidMsgRecv(notification:)),name: wxNotifyChatMsgRecv, object: nil)
    }
    
    //微信通知处理函数
    func wxdidMsgRecv(notification:NSNotification){
        
        print("微信传过来的授权数据")
        print("didMsgRecv: \(notification.userInfo)")
        
        guard let dic = notification.userInfo as? [String : Any] else {
            return
        }
        
        let wxLocalSaveData = dic as NSDictionary
        
        if wxLocalSaveData.count == 0 {
            return
        }
        
        
        print(wxLocalSaveData["wxid"] as Any,wxLocalSaveData["wxheadImgURL"] as Any,wxLocalSaveData["wxnickName"] as Any)
        
        ///获取微信open id
        let wxopenID = wxLocalSaveData["wxid"] as! String
        
        ///获取微信头像地址
        let wxHeadImgURL = wxLocalSaveData["wxheadImgURL"] as! String
        
        ///微信昵称
        let wxNickName = wxLocalSaveData["wxnickName"] as! String
        
        
        let param = ["deviceid" : deviceID,
                     "devtype" : deviceTpye,
                     "openid" : wxopenID,
                     "platform" : 2,
                     "headimg" : wxHeadImgURL,
                     "nickname" : wxNickName,] as [String :Any]
        
        print("wxlogin---")
        
        postWithPath(path: oauthURL, paras: param, success: { (response) in
            
            print("wxlogin---")
            
            guard let dic = response as? NSDictionary else {
                return
            }
            
            print(dic)
            
            //提取提示语
            let alertmsg = dic["resultmsg"] as! String
            
            
            if alertmsg == "授权成功" {
                MBManager.showBriefAlert("授权登陆成功")
                
                print("------")
                
                print((dic["data"] as! NSDictionary)["user"] as! NSDictionary)
                guard let list = (dic["data"] as? NSDictionary)?["user"] as? NSDictionary else {
                    return
                }
                
                //取出登陆后的token
                guard let tokenData = dic["data"] as? NSDictionary else {
                    return
                }
                
                let token = tokenData["token"] as! String
                
                print("token",token)
                
                //将token存入本地
                localSave.set(token, forKey: userToken)
                localSave.synchronize()
                
                
                //list数组为空，就返回
                if list.count == 0 {
                    return
                }
                
                let wxImg = list["img"] as! String
                let wxNickName = list["username"] as! String
                
                print(wxImg,wxNickName)
                
                //发送通知
                
                //通知名称常量
                let notifyChatMsgRecv = NSNotification.Name(rawValue:wxNotifi)
                NotificationCenter.default.post(name:notifyChatMsgRecv, object: nil, userInfo: ["wxImgUrl" : wxImg,
                                                                                                "wxNickName": wxNickName] as [String : Any])
            }
            
        }, failure: { (error) in
            print(error.localizedDescription)
        })
        
    }
}
